<?php

class LabelsController extends Controller
{
	public function actionTest($slug)
	{
		$model = Labels::model()->findByAttributes(array('slug'=>$slug));
		
		$rows = Yii::app()->db->createCommand()
            ->select('l.*, e.*, n.*, t.*')
            ->from('labels l')
            ->join('events e','t.event_id = e.id')
            ->join('news n', 't.event_id = n.id')
			->join('event_labels t', 't.label_id = l.id')
            ->where('l.slug=:slug', array(':slug'=>$slug))
            ->order('e.posteddate, n.posteddate')
            ->queryRow();
		var_dump($rows);
	}
	
	public function actionIndex()
	{
		$model = new Labels;
		/*
		$criteria = new CDbCriteria;
		$criteria->condition = 'status="A" AND enddate >= NOW()';
		*/
		$dataProvider=new CActiveDataProvider('Labels',
			array(
				//'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'posteddate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		$this->render('index', array(
			'dataProvider'=>$dataProvider,
			'model'=>$model
		));
	}
	
	public function actionView($slug)
	{
		$label = new EventLabels;
		$model = Labels::model()->findByAttributes(array('slug'=>$slug));
		
		$criteria = new CDbCriteria;
		$criteria->condition = 'status="A" AND enddate >= NOW()';
		$criteria->with = array('eventLabel'=>array(
			'joinType'=>'INNER JOIN',               
			'condition'=>'eventLabel.label_id = '.$model->id
		));

		$dataProviderEvent=new CActiveDataProvider('Events',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'posteddate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		
		$criteria = new CDbCriteria;
		$criteria->condition = 'status="A"';
		$criteria->with = array('newsLabel'=>array(
			'joinType'=>'INNER JOIN',               
			'condition'=>'newsLabel.label_id = '.$model->id
		));

		$dataProviderNews=new CActiveDataProvider('News',
			array(
				'criteria'=>$criteria,
				'sort'=>array(
					'defaultOrder'=>'posteddate DESC',
				),
				'pagination'=>array(
					'pageSize'=>10,
					'pageVar'=>'page',
				),
			)
		);
		
		$this->render('view', array(
			'dataProviderEvent'=>$dataProviderEvent,
			'dataProviderNews'=>$dataProviderNews,
			'model'=>$model,
			'label'=>$label
		));
	}
	
	public function actionCreate()
	{
		if (isset($_POST['Labels'])) {
			$model = new Labels;
			$model->attributes = $_POST['Labels'];
			$model->slug = $this->createSlug($model->title);
			$model->posteddate = $model->updatedate = date('Y-m-d H:i:s');
			
			if ($model->validate() && $model->save()) {
				$this->redirect(array('/Events/labels/index'));
			}
		}
	}
	
	public function actionDelete($id)
	{
		$model = Labels::model()->findByPk($id)->delete();

		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function actionAddEvent($slug)
	{
		$model = new EventLabels;
		if (isset($_POST['EventLabels'])) {
			$model->attributes = $_POST['EventLabels'];
			if ($model->save()) {
				$this->redirect(array('/Events/labels/view', 'slug'=>$slug));
			}
		}
	}
	public function actionRemoveEvent($id)
	{
		$model = EventLabels::model()->findByAttributes(array('event_id'=>$id))->delete();
		
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
}